package com.ruoyi.project.applet.controller;

import java.text.ParseException;
import java.util.List;

import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.project.applet.domain.VipLevel;
import com.ruoyi.project.applet.mapper.VipLevelMapper;
import com.ruoyi.project.applet.service.InteractService;
import com.ruoyi.project.applet.service.RecordService;
import com.ruoyi.project.system.domain.SysUserWx;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.applet.domain.Vip;
import com.ruoyi.project.applet.service.IVipService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 会员Controller
 *
 * @author ruoyi
 * @date 2025-06-01
 */
@RestController
@RequestMapping("/applet/vip")
public class VipController extends BaseController
{
    @Autowired
    private IVipService vipService;

    @Autowired
    private RecordService recordService;

    @Autowired
    private InteractService interactService;

    @Autowired
    private VipLevelMapper vipLevelMapper;

    /**
     * 查询会员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Vip vip)
    {
        startPage();
        List<Vip> list = vipService.selectVipList(vip);
        return getDataTable(list);
    }



    /**
     * 获取会员详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") int id)
    {
        return AjaxResult.success(vipService.selectVipById(id));
    }

    /**
     * 新增会员
     */
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Vip vip)
    {
        LoginUser loginUser = getLoginUser();
        vip.setCreateBy(loginUser.getUser().getUserName());
        return toAjax(vipService.insertVip(vip));
    }

    /**
     * 修改会员
     */
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Vip vip)
    {
        LoginUser loginUser = getLoginUser();
        vip.setUpdateBy(loginUser.getUser().getUserName());
        return toAjax(vipService.updateVip(vip));
    }

    /**
     * 删除会员
     */
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vipService.deleteVipByIds(ids));
    }


    //获取聊天记录
    @ApiOperation("根据用户Id和对话Id查询聊天记录")
    @PostMapping("/selectBychatid")
    public AjaxResult selectBychatid(Integer from_id, Integer id){
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        return recordService.selectBychatid(userWx.getUserId(),from_id,id);
    }

    @ApiOperation("根据用户Id查询聊天记录")
    @PostMapping("/selectByto_id")
    public AjaxResult selectByto_id() throws ParseException {
//        HttpSession session = request.getSession();
//        Long to_id = (Long) session.getAttribute("accountId");
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        return interactService.selectByto_id(userWx.getUserId());
    }

    @ApiOperation("根据用户Id查询未读消息条数")
    @GetMapping("/selectCount")
    public AjaxResult selectCount() throws ParseException {
//        HttpSession session = request.getSession();
//        Long to_id = (Long) session.getAttribute("accountId");
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        return interactService.selectCount(userWx.getUserId());
    }

    @ApiOperation("新增聊天记录")
    @PostMapping("/insertInteract")
    public AjaxResult insertInteract(String ss) throws ParseException {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx,"请先登录");
        return interactService.insertInteract(ss,userWx.getUserId());
    }

    @ApiOperation("查询会员等级")
    @GetMapping("/selectVipLevel")
    public AjaxResult selectVipLevel() {
        return AjaxResult.success(vipLevelMapper.selectVipLevelList(new VipLevel()));
    }
}